import Vue from 'vue'
import App from './App'
import * as localConfig from "./config/local/config.js"
import TIM from './common/tim-wx.js';
import TIMUploadPlugin from './common/tim-upload-plugin.js';
import timeFormat from "./common/timeFormat.js"
Vue.config.productionTip = false
let tim = TIM.create({
	SDKAppID: 1400562688
})
tim.registerPlugin({
	'tim-upload-plugin': TIMUploadPlugin
});
// console.log(TIMUploadPlugin);
uni.$tim = tim
uni.$TIM = TIM


import store from './store'
Vue.prototype.$timeFormat = timeFormat
Vue.prototype.$store = store

import uView from 'uview-ui'
Vue.use(uView);

Vue.prototype.localConfig = localConfig

// 引入request库
import $H from '@/common/lib/request.js';
uni.$H = $H
Vue.prototype.$H = $H

uni.$timlogin = function() {
	console.log(uni.getStorageSync('user_id'));
	$H.post("admin/user/tim/sign").then(res => {
		let promise = tim.login({
			userID: uni.getStorageSync('user_id').toString(),
			userSig: res.data.sign
		})
		promise.then((imResponse) => {
			console.log(imResponse.data); // 登录成功
			if (imResponse.data.repeatLogin === true) {
				// 标识账号已登录，本次登录操作为重复登录。v2.5.1 起支持
				// console.log(imResponse.data.errorInfo);
			}
			// uni.$tim.on('TIM.EVENT.SDK_READY', this.chatList);
		}).catch((imError) => {
			console.warn('login error:', imError); // 登录失败的相关信息
		});
	}).catch(err => {
		console.log(err);
	})
}

let onMessageReceived = function(event) {
	// console.log(event.data);
	var pages = getCurrentPages();
	var page = pages[pages.length - 1];
	if (event.data[0].type == 'TIMCustomElem') {
		console.log(event.data[0].payload);
		if (event.data[0].payload.data == 'videomsg') {
			if (event.data[0].payload.description == 1) {
				var obj = JSON.parse(event.data[0].payload.extension)
				$H.post("admin/user/tim/sign").then(res => {
					uni.navigateTo({
						url: `/pagesA/kefu/live?myname=${obj.myname}&myhead=${obj.myhead}&youname=${obj.youname}&youhead=${obj.youhead}&roomID=${obj.roomID}&sign=${res.data.sign}&to_id=${obj.to_id}&type=video`,
						fail: (err) => {
							console.log(err);
						}
					})
				}).catch(err => {
					console.log(err);
				})
			} else {
				console.log(page.route);
				if (page.route == 'pagesA/kefu/live') {
					uni.navigateBack()
				}
			}
		} else if (event.data[0].payload.data == 'voicemsg') {
			if (event.data[0].payload.description == 1) {
				console.log(event.data[0].payload);
				var obj = JSON.parse(event.data[0].payload.extension)
				$H.post("admin/user/tim/sign").then(res => {
					uni.navigateTo({
						url: `/pagesA/kefu/live?myname=${obj.myname}&myhead=${obj.myhead}&youname=${obj.youname}&youhead=${obj.youhead}&roomID=${obj.roomID}&sign=${res.data.sign}&to_id=${obj.to_id}&type=voice`,
						fail: (err) => {
							console.log(err);
						}
					})
				}).catch(err => {
					console.log(err);
				})
			} else {
				if (page.route == 'pagesA/kefu/live') {
					uni.navigateBack()
				}
			}
		}
	} else {
		// #ifdef APP-PLUS
		if (page.route != 'pagesA/kefu/kefu' && page.route != "pagesA/kefu/list") {
			plus.push.createMessage("您有一个新的消息，请注意查看");
			uni.showToast({
				icon: "none",
				title: "您有一个新的消息，请注意查看"
			})
		}
		// #endif
	}
};
tim.on(TIM.EVENT.MESSAGE_RECEIVED, onMessageReceived);

let onKickedOut = function(event) {
	console.log(event.data.type); // mutipleAccount(同一设备，同一帐号，多页面登录被踢)
	uni.showToast({
		icon: "none",
		title: "该账号已在其他设备登录"
	})
	uni.removeStorageSync('token');
	uni.removeStorageSync('user_id');
	uni.reLaunch({
		url: "/pages/login/login"
	})
};
tim.on(TIM.EVENT.KICKED_OUT, onKickedOut);

App.mpType = 'app'

const app = new Vue({
	...App,
	store
})
app.$mount()